#ifndef __SGPIO_H__
#define __SGPIO_H__



#define	MODE_G_IN	  0
#define	MODE_G_OUT	1
#define	MODE_AF    	2
#define	MODE_AN  		3

#define  OType_PP    0
#define  OType_OD 	1

#define  SPEED_LEVEL_1  1 /*!< Medium Speed */
#define  SPEED_LEVEL_2  2 /*!< Fast Speed   */
#define  SPEED_LEVEL_3  3  /*!< High Speed   */



#define GPIOA_MODER (*((volatile unsigned int *)(GPIOA_BASE+0x00)))
#define GPIOB_MODER (*((volatile unsigned int *)(GPIOB_BASE+0x00)))
#define GPIOC_MODER (*((volatile unsigned int *)(GPIOC_BASE+0x00)))
#define GPIOD_MODER (*((volatile unsigned int *)(GPIOD_BASE+0x00)))
#define GPIOE_MODER (*((volatile unsigned int *)(GPIOE_BASE+0x00)))
#define GPIOF_MODER (*((volatile unsigned int *)(GPIOF_BASE+0x00)))

#define GPIOA_OTYPER (*((volatile unsigned int *)(GPIOA_BASE+0x04)))
#define GPIOB_OTYPER (*((volatile unsigned int *)(GPIOB_BASE+0x04)))
#define GPIOC_OTYPER (*((volatile unsigned int *)(GPIOC_BASE+0x04)))
#define GPIOD_OTYPER (*((volatile unsigned int *)(GPIOD_BASE+0x04)))
#define GPIOE_OTYPER (*((volatile unsigned int *)(GPIOE_BASE+0x04)))
#define GPIOF_OTYPER (*((volatile unsigned int *)(GPIOF_BASE+0x04)))

#define GPIOA_OSPEEDR (*((volatile unsigned int *)(GPIOA_BASE+0x08)))
#define GPIOB_OSPEEDR (*((volatile unsigned int *)(GPIOB_BASE+0x08)))
#define GPIOC_OSPEEDR (*((volatile unsigned int *)(GPIOC_BASE+0x08)))
#define GPIOD_OSPEEDR (*((volatile unsigned int *)(GPIOD_BASE+0x08)))
#define GPIOE_OSPEEDR (*((volatile unsigned int *)(GPIOE_BASE+0x08)))
#define GPIOF_OSPEEDR (*((volatile unsigned int *)(GPIOF_BASE+0x08)))

#define GPIOA_PUPDR (*((volatile unsigned int *)(GPIOA_BASE+0x0C)))
#define GPIOB_PUPDR (*((volatile unsigned int *)(GPIOB_BASE+0x0C)))
#define GPIOC_PUPDR (*((volatile unsigned int *)(GPIOC_BASE+0x0C)))
#define GPIOD_PUPDR (*((volatile unsigned int *)(GPIOD_BASE+0x0C)))
#define GPIOE_PUPDR (*((volatile unsigned int *)(GPIOE_BASE+0x0C)))
#define GPIOF_PUPDR (*((volatile unsigned int *)(GPIOF_BASE+0x0C)))
	
#define GPIOA_IDR (*((volatile unsigned int *)(GPIOA_BASE+0x10)))
#define GPIOB_IDR (*((volatile unsigned int *)(GPIOB_BASE+0x10)))
#define GPIOC_IDR (*((volatile unsigned int *)(GPIOC_BASE+0x10)))
#define GPIOD_IDR (*((volatile unsigned int *)(GPIOD_BASE+0x10)))
#define GPIOE_IDR (*((volatile unsigned int *)(GPIOE_BASE+0x10)))
#define GPIOF_IDR (*((volatile unsigned int *)(GPIOF_BASE+0x10)))

#define GPIOA_ODR (*((volatile unsigned int *)(GPIOA_BASE+0x14)))
#define GPIOB_ODR (*((volatile unsigned int *)(GPIOB_BASE+0x14)))
#define GPIOC_ODR (*((volatile unsigned int *)(GPIOC_BASE+0x14)))
#define GPIOD_ODR (*((volatile unsigned int *)(GPIOD_BASE+0x14)))
#define GPIOE_ODR (*((volatile unsigned int *)(GPIOE_BASE+0x14)))
#define GPIOF_ODR (*((volatile unsigned int *)(GPIOF_BASE+0x14)))

#define GPIOA_BSRR (*((volatile unsigned int *)(GPIOA_BASE+0x18)))
#define GPIOB_BSRR (*((volatile unsigned int *)(GPIOB_BASE+0x18)))
#define GPIOC_BSRR (*((volatile unsigned int *)(GPIOC_BASE+0x18)))
#define GPIOD_BSRR (*((volatile unsigned int *)(GPIOD_BASE+0x18)))
#define GPIOE_BSRR (*((volatile unsigned int *)(GPIOE_BASE+0x18)))
#define GPIOF_BSRR (*((volatile unsigned int *)(GPIOF_BASE+0x18)))

#define GPIOA_LCKR (*((volatile unsigned int *)(GPIOA_BASE+0x1C)))
#define GPIOB_LCKR (*((volatile unsigned int *)(GPIOB_BASE+0x1C)))
#define GPIOC_LCKR (*((volatile unsigned int *)(GPIOC_BASE+0x1C)))
#define GPIOD_LCKR (*((volatile unsigned int *)(GPIOD_BASE+0x1C)))
#define GPIOE_LCKR (*((volatile unsigned int *)(GPIOE_BASE+0x1C)))
#define GPIOF_LCKR (*((volatile unsigned int *)(GPIOF_BASE+0x1C)))

#define GPIOA_AFRL (*((volatile unsigned int *)(GPIOA_BASE+0x20)))
#define GPIOB_AFRL (*((volatile unsigned int *)(GPIOB_BASE+0x20)))
#define GPIOC_AFRL (*((volatile unsigned int *)(GPIOC_BASE+0x20)))
#define GPIOD_AFRL (*((volatile unsigned int *)(GPIOD_BASE+0x20)))
#define GPIOE_AFRL (*((volatile unsigned int *)(GPIOE_BASE+0x20)))
#define GPIOF_AFRL (*((volatile unsigned int *)(GPIOF_BASE+0x20)))

#define GPIOA_AFRH (*((volatile unsigned int *)(GPIOA_BASE+0x24)))
#define GPIOB_AFRH (*((volatile unsigned int *)(GPIOB_BASE+0x24)))
#define GPIOC_AFRH (*((volatile unsigned int *)(GPIOC_BASE+0x24)))
#define GPIOD_AFRH (*((volatile unsigned int *)(GPIOD_BASE+0x24)))
#define GPIOE_AFRH (*((volatile unsigned int *)(GPIOE_BASE+0x24)))
#define GPIOF_AFRH (*((volatile unsigned int *)(GPIOF_BASE+0x24)))

#define GPIOA_BRR (*((volatile unsigned int *)(GPIOA_BASE+0x28)))
#define GPIOB_BRR (*((volatile unsigned int *)(GPIOB_BASE+0x28)))
#define GPIOC_BRR (*((volatile unsigned int *)(GPIOC_BASE+0x28)))
#define GPIOD_BRR (*((volatile unsigned int *)(GPIOD_BASE+0x28)))
#define GPIOE_BRR (*((volatile unsigned int *)(GPIOE_BASE+0x28)))
#define GPIOF_BRR (*((volatile unsigned int *)(GPIOF_BASE+0x28)))

//GPIO input
#define GPIOA_IN GPIOA_IDR
#define GPIOB_IN GPIOB_IDR
#define GPIOC_IN GPIOC_IDR
#define GPIOD_IN GPIOD_IDR
#define GPIOE_IN GPIOE_IDR
#define GPIOF_IN GPIOF_IDR

//Bit field of GPIOx_MODER
typedef struct bGPIOx_MODER
{
unsigned int MODE0 :2;
unsigned int MODE1 :2;
unsigned int MODE2 :2;
unsigned int MODE3 :2;
unsigned int MODE4 :2;
unsigned int MODE5 :2;
unsigned int MODE6 :2;
unsigned int MODE7 :2;
unsigned int MODE8 :2;
unsigned int MODE9 :2;
unsigned int MODE10 :2;
unsigned int MODE11 :2;
unsigned int MODE12 :2;
unsigned int MODE13 :2;
unsigned int MODE14 :2;
unsigned int MODE15 :2;
}bGPIOx_MODER;
//Bit field of GPIOx_MODER
typedef struct bGPIOx_OSPEEDR
{
unsigned int OSPEEDR0 :2;
unsigned int OSPEEDR1 :2;
unsigned int OSPEEDR2 :2;
unsigned int OSPEEDR3 :2;
unsigned int OSPEEDR4 :2;
unsigned int OSPEEDR5 :2;
unsigned int OSPEEDR6 :2;
unsigned int OSPEEDR7 :2;
unsigned int OSPEEDR8 :2;
unsigned int OSPEEDR9 :2;
unsigned int OSPEEDR10 :2;
unsigned int OSPEEDR11 :2;
unsigned int OSPEEDR12 :2;
unsigned int OSPEEDR13 :2;
unsigned int OSPEEDR14 :2;
unsigned int OSPEEDR15 :2;
}bGPIOx_OSPEEDR;


//GPIOA MODE
#define GPIOA_MODE0 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE0)
#define GPIOA_MODE1 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE1)
#define GPIOA_MODE2 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE2)
#define GPIOA_MODE3 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE3)
#define GPIOA_MODE4 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE4)
#define GPIOA_MODE5 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE5)
#define GPIOA_MODE6 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE6)
#define GPIOA_MODE7 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE7)
#define GPIOA_MODE8 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE8)
#define GPIOA_MODE9 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE9)
#define GPIOA_MODE10 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE10)
#define GPIOA_MODE11 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE11)
#define GPIOA_MODE12 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE12)
#define GPIOA_MODE13 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE13)
#define GPIOA_MODE14 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE14)
#define GPIOA_MODE15 (((volatile bGPIOx_MODER *)(&GPIOA_MODER))->MODE15)

//GPIOA OTYPE
//#define GPIOA_OTYPER0 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE0)
//#define GPIOA_OTYPER1 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE1)
//#define GPIOA_OTYPER2 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE2)
//#define GPIOA_OTYPER3 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE3)
//#define GPIOA_OTYPER4 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE4)
//#define GPIOA_OTYPER5 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE5)
//#define GPIOA_OTYPER6 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE6)
//#define GPIOA_OTYPER7 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE7)
//#define GPIOA_OTYPER8 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE8)
//#define GPIOA_OTYPER9 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE9)
//#define GPIOA_OTYPER10 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE10)
//#define GPIOA_OTYPER11 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE11)
//#define GPIOA_OTYPER12 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE12)
//#define GPIOA_OTYPER13 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE13)
//#define GPIOA_OTYPER14 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE14)
//#define GPIOA_OTYPER15 (((volatile bGPIOx_MODER *)(&GPIOA_OTYPER))->MODE15)

//GPIOA OSPEEDR
#define GPIOA_OSPEEDR0 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR0)
#define GPIOA_OSPEEDR1 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR1)
#define GPIOA_OSPEEDR2 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR2)
#define GPIOA_OSPEEDR3 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR3)
#define GPIOA_OSPEEDR4 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR4)
#define GPIOA_OSPEEDR5 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR5)
#define GPIOA_OSPEEDR6 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR6)
#define GPIOA_OSPEEDR7 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR7)
#define GPIOA_OSPEEDR8 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR8)
#define GPIOA_OSPEEDR9 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR9)
#define GPIOA_OSPEEDR10 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR10)
#define GPIOA_OSPEEDR11 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR11)
#define GPIOA_OSPEEDR12 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR12)
#define GPIOA_OSPEEDR13 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR13)
#define GPIOA_OSPEEDR14 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR14)
#define GPIOA_OSPEEDR15 (((volatile bGPIOx_OSPEEDR *)(&GPIOA_OSPEEDR))->OSPEEDR15)



//GPIOA input bit field
#define GPIOA_IN0 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN0)
#define GPIOA_IN1 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN1)
#define GPIOA_IN2 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN2)
#define GPIOA_IN3 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN3)
#define GPIOA_IN4 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN4)
#define GPIOA_IN5 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN5)
#define GPIOA_IN6 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN6)
#define GPIOA_IN7 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN7)
#define GPIOA_IN8 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN8)
#define GPIOA_IN9 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN9)
#define GPIOA_IN10 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN10)
#define GPIOA_IN11 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN11)
#define GPIOA_IN12 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN12)
#define GPIOA_IN13 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN13)
#define GPIOA_IN14 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN14)
#define GPIOA_IN15 (((volatile bGPIOx_IN *)(&GPIOA_IN))->IN15)
	
//GPIOx set
#define GPIOA_SET GPIOA_BSRR
#define GPIOB_SET GPIOB_BSRR
#define GPIOC_SET GPIOC_BSRR
#define GPIOD_SET GPIOD_BSRR
#define GPIOE_SET GPIOE_BSRR
#define GPIOF_SET GPIOF_BSRR
//GPIOx clr
#define GPIOA_CLR GPIOA_BRR
#define GPIOB_CLR GPIOB_BRR
#define GPIOC_CLR GPIOC_BRR
#define GPIOD_CLR GPIOD_BRR
#define GPIOE_CLR GPIOE_BRR
#define GPIOF_CLR GPIOF_BRR

#endif
